import { useUserStore } from '@/stores/user'
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/software'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue')
  },
  {
    path: '/software',
    name: 'SoftwareSelection',
    component: () => import('@/views/SoftwareSelection.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/script',
    name: 'ScriptGeneration',
    component: () => import('@/views/ScriptGeneration.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) =>
{
  const userStore = useUserStore()

  if (to.meta.requiresAuth && !userStore.isAuthenticated)
  {
    next('/login')
  } else
  {
    next()
  }
})

export default router
